//
//  AESCrypt.m
//  Gurpartap Singh
//
//  Created by Gurpartap Singh on 06/05/12.
//  Copyright (c) 2012 Gurpartap Singh
// 
// 	MIT License
// 
// 	Permission is hereby granted, free of charge, to any person obtaining
// 	a copy of this software and associated documentation files (the
// 	"Software"), to deal in the Software without restriction, including
// 	without limitation the rights to use, copy, modify, merge, publish,
// 	distribute, sublicense, and/or sell copies of the Software, and to
// 	permit persons to whom the Software is furnished to do so, subject to
// 	the following conditions:
// 
// 	The above copyright notice and this permission notice shall be
// 	included in all copies or substantial portions of the Software.
// 
// 	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// 	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// 	MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// 	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// 	LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// 	OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// 	WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

#import "AESCrypt.h"

#import "NSData+Base64.h"
#import "NSString+Base64.h"
#import "NSData+CommonCrypto.h"

@implementation AESCrypt

+ (NSString *)encrypt:(NSString *)message password:(NSString *)password {
  NSData *encryptedData = [[message dataUsingEncoding:NSUTF8StringEncoding] AES256EncryptedDataUsingKey:[[password dataUsingEncoding:NSUTF8StringEncoding] SHA256Hash] error:nil];
  NSString *base64EncodedString = [NSString base64StringFromData:encryptedData length:[encryptedData length]];
  return base64EncodedString;
}

+ (NSString *)decrypt:(NSString *)base64EncodedString password:(NSString *)password {
  NSData *encryptedData = [NSData base64DataFromString:base64EncodedString];
  NSData *decryptedData = [encryptedData decryptedAES256DataUsingKey:[[password dataUsingEncoding:NSUTF8StringEncoding] SHA256Hash] error:nil];
  return [[NSString alloc] initWithData:decryptedData encoding:NSUTF8StringEncoding];
}

// Answern
+ (NSString *)answernEncrypt:(NSString *)oriValue {
    
    NSString *charStr = @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    NSString *key = @"";
    for (int i = 0; i < 6; i++) {
        NSInteger ran = arc4random() % [charStr length];
        key = [key stringByAppendingString:[charStr substringWithRange:NSMakeRange(ran, 1)]];
    }
//    NSLog(@"key = %@",key);
    return [AESCrypt answernEncrypt:oriValue key:key];
}

+ (NSString *)answernEncrypt:(NSString *)oriValue key:(NSString *)key {
    
    // key随机生成A-Za-z0-9
    // key=abcdef
    // value=123456789
    // 一个key，一个value拼接，key短补1，key长补2
    
    NSString *newValue = @"";
    if (oriValue.length >= key.length) {
        
        for(int i = 0; i < [key length]; i++) {
            
            newValue = [newValue stringByAppendingString:[key substringWithRange:NSMakeRange(i, 1)]];
            newValue = [newValue stringByAppendingString:[oriValue substringWithRange:NSMakeRange(i, 1)]];
        }
        newValue = [newValue stringByAppendingString:[oriValue substringWithRange:NSMakeRange(key.length, oriValue.length - key.length)]];
        newValue = [newValue stringByAppendingString:@"1"];
        
    } else {
        
        for(int i = 0; i < [oriValue length]; i++) {
            
            newValue = [newValue stringByAppendingString:[key substringWithRange:NSMakeRange(i, 1)]];
            newValue = [newValue stringByAppendingString:[oriValue substringWithRange:NSMakeRange(i, 1)]];
        }
        newValue = [newValue stringByAppendingString:[key substringWithRange:NSMakeRange(oriValue.length, key.length - oriValue.length)]];
        newValue = [newValue stringByAppendingString:@"2"];

    }
//    NSLog(@"newValue = %@",newValue);
    
    NSData *data = [newValue dataUsingEncoding:NSUTF8StringEncoding];
    NSString *enValue = [NSString base64StringFromData:data length:[data length]];
    
    NSMutableString *mStr = [[NSMutableString alloc] initWithString:enValue];
    [mStr insertString:key atIndex:4];
    
    return mStr;
}

+ (NSString *)answernDecrypt:(NSString *)enValue {

    if (enValue.length < 10) {
        return @"";
    }
    NSString *key = [enValue substringWithRange:NSMakeRange(4, 6)];
    NSString *newValue = [enValue stringByReplacingCharactersInRange:NSMakeRange(4, 6) withString:@""];
//    NSLog(@"answernDecrypt = %@",newValue);
    return [AESCrypt answernDecrypt:newValue key:key];
}

+ (NSString *)answernDecrypt:(NSString *)enValue key:(NSString *)key {
    
    NSData *encryptedData = [NSData base64DataFromString:enValue];

    NSString *deValue = [[NSString alloc] initWithData:encryptedData encoding:NSUTF8StringEncoding];
    
    NSMutableString *mStr = [[NSMutableString alloc] initWithString:deValue];
    if (mStr.length < 6) {
        return @"";
    }
    for (int i = 0; i < 6; i++) {
        [mStr deleteCharactersInRange:NSMakeRange(i, 1)];
    }
    if (mStr.length > 1) {
        [mStr deleteCharactersInRange:NSMakeRange(mStr.length-1, 1)];
    }

    return mStr;
}
@end
